package com.vogtec.fota.entity.mqtt;

import java.io.UnsupportedEncodingException;

/**
 * Created by TieYan on 2017/1/10.
 */
public class Util {
    public static byte[] string2AsciiArray(String s){
        if(s == null || s.length() == 0){
            return null;
        }
        char[] chars = s.toCharArray();
        byte[] result = new byte[chars.length];
        for(int i = 0; i < chars.length; i++){
            result[i] = (byte)chars[i];
        }
        return result;
    }

    public static double byteArray2Double(byte[] array, int offset, int length) throws UnsupportedEncodingException {
        if(offset + length > array.length){
            throw new StringIndexOutOfBoundsException("offset + length > array.length");
        }
        byte[] bytes = new byte[length];
        for(int i = 0; i < length; i++){
            bytes[i] = array[i + offset];
        }
        return Double.valueOf(new String(bytes, "ASCII"));
    }

    public static int byteArray2Int(byte[] array, int offset, int length) throws UnsupportedEncodingException {
        if(offset + length > array.length){
            throw new StringIndexOutOfBoundsException("offset + length > array.length");
        }
        byte[] bytes = new byte[length];
        for(int i = 0; i < length; i++){
            bytes[i] = array[i + offset];
        }
        return Integer.valueOf(new String(bytes, "ASCII"));
    }

    public static String byteArray2String(byte[] array, int offset, int length) throws UnsupportedEncodingException {
        if(offset + length > array.length){
            throw new StringIndexOutOfBoundsException("offset + length > array.length");
        }
        byte[] bytes = new byte[length];
        for(int i = 0; i < length; i++){
            bytes[i] = array[i + offset];
        }
        return new String(bytes, "ASCII");
    }

    public static byte[] add(byte a, byte[] b){
        byte[] result = new byte[b.length + 1];
        result[0] = a;
        for(int i = 0; i < b.length; i++){
            result[i + 1] = b[i];
        }
        return result;
    }

    public static byte[] add(byte[] a, byte[] b){
        byte[] result = new byte[a.length + b.length];
        for(int i = 0; i < a.length; i++){
            result[i] = a[i];
        }
        for(int i = 0; i < b.length; i++){
            result[a.length + i] = b[i];
        }
        return result;
    }

    public static byte[] add(byte[] a, byte b){
        byte[] result = new byte[a.length + 1];
        for(int i = 0; i < a.length; i++){
            result[i] = a[i];
        }
        result[a.length] = b;
        return result;
    }

    @Deprecated
    public static byte[] intToByteArray(final int integer) {
        int byteNum = (40 - Integer.numberOfLeadingZeros(integer < 0 ? ~integer : integer)) / 8;
        byte[] byteArray = new byte[4];

        for (int n = 0; n < byteNum; n++)
            byteArray[3 - n] = (byte) (integer >>> (n * 8));

        return (byteArray);
    }

    @Deprecated
    public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
        byte[] byte_3 = new byte[byte_1.length + byte_2.length];
        System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
        System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
        return byte_3;
    }
    @Deprecated
    public static int byteArrayToInt(byte[] b, int offset) {
        int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i + offset] & 0x000000FF) << shift;
        }
        return value;
    }
}
